package com.zhiyou.controller;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhiyou.pojo.User;
import com.zhiyou.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {
    //第一种  获得页面传来的值
	@RequestMapping("logins")
	public String login(HttpServletRequest request) {
		//System.out.println("用户登录==login");
		String username=request.getParameter("username");
		String pwd=request.getParameter("pwd");
		System.out.println("controller 接受的数据");
		System.out.println("username  "+username);
		System.out.println("pwd  "+pwd);
		return "success";
	}
	//自动注入 对象
	@Autowired
	UserService userService;
	
	//第二种  传参方式
	@RequestMapping("login")
	public String login(String username,String pwd) {
		System.out.println("controller 接受的数据");
		System.out.println("username  "+username);
		System.out.println("pwd  "+pwd);
		boolean flag=userService.login(username,pwd);
		if (flag) {
			return "success";
		}else {
			return "error";
		}
		
		
	}
	
	
	//第三种传参方式
	@RequestMapping("login")
	public String login2(@RequestParam("username")String name,String pwd) {
		System.out.println("controller 接受的数据");
		System.out.println("name  "+name);
		System.out.println("pwd  "+pwd);
		boolean flag=userService.login(name,pwd);
		return "success";
	}
	
	//第四种传参方式
	@RequestMapping("register1")
	public String register1(String username,String pwd,String address,String phone) {
		System.out.println("controller 接受的数据");
		System.out.println("username  "+username);
		System.out.println("pwd  "+pwd);
		System.out.println("address  "+address);
		System.out.println("phone  "+phone);
		return "success";
	}
	
	//第四种传参方式  对象
	@RequestMapping("register")
	public String register(User user) {
		System.out.println("controller 接受的数据");
		System.out.println("username  "+user.getUsername());
		System.out.println("pwd  "+user.getPwd());
		System.out.println("address  "+user.getAddress());
		System.out.println("phone  "+user.getPhone());
		return "success";
	}
	
	
	
	//例子  根据ID 进行查询  向前端发送数据
	
	//第一种   以前的原生态
	@RequestMapping(value="getById")
	public String getUserById(int id,HttpServletRequest request) {
        //返回一个 username Tom
		request.setAttribute("username", "tom");
		return "success";
	}
	
	//第二种  map 方式
	@RequestMapping(value="getById2")
	public String getUserById2(int id,Map<String ,Object> map) {
        //返回一个 username Tom
		map.put("username","tom");
		User user=new User("tom","123","110","japan");
		map.put("user",user);
		return "success";
	}
	
	
	//根据ID删除数据
	@RequestMapping("delete/{id}")
	public String deleteUser(@PathVariable("id")Integer id) {
		System.out.println("id  "+id);
		return "success";
	}
	
	
	/*@RequestMapping("delete")
	public String deleteUser() {
	    System.out.println("====user delete 用户删除==");		
		return "success";
	}*/
}
